import json

from common.openai_ali import get_client


llm = get_client()

def get_province_details(province:str,population:str,cities:list):
    print(province,population,cities)
    return province,population,cities

province_tools  = [
    {
        "type": "function",  # 添加 type 字段
        "function": {
            "name": "get_province_details",
            "description": "获取省份的详细信息",  # 添加描述
            "parameters": {
                "type": "object",
                "properties": {
                    "cities": {
                        "type": "array",  # 将 list 改为 array
                        "items": {
                            "type": "string"
                        }
                    },
                    "province": {
                        "type": "string"
                    },
                    "population": {
                        "type": "string"
                    }
                },
                "required": ["province","population","cities"]  # 添加 required 字段
            }
        }
    }
]


prompt = '江苏省的人口、省份及城市，以json格式返回'
response = llm.chat.completions.create( model="qwen-plus",
                                        messages=[{"role":"user","content":prompt}],
                                        tools=province_tools,
                                        tool_choice="auto"
                                        )

print(response.choices[0].message.tool_calls)
# # 检查是否有工具调用
# if response.choices[0].message.tool_calls:
#     tool_call = response.choices[0].message.tool_calls[0]
#     function_args = json.loads(tool_call.function.arguments)
#     get_province_details(**function_args)
#     print("函数参数:", function_args)
# else:
#     print("模型直接回复:", response.choices[0].message.content)
# print(json.loads(response.choices[0].message.tool_calls[0].function.name))